package com.maxmlr.net.rpc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName TcpService
 * @Description TODO
 * @Author maxiao
 * @Date 2020/4/4 下午7:42
 * @Version V1.0
 */
public class TcpServer {

    private static ExecutorService executorService = new ThreadPoolExecutor(
            3, 100, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    public void publisher(Object sevice, int port) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                System.out.println("等待客户机连接");
                Socket socket = serverSocket.accept();
                executorService.execute(new ProcessorHandler(sevice, socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
            }
        }

    }


}
